package miniproj.services.data;

import java.io.*;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 * This class is able to load xml data using the JAXB library
 * 
 * @author Baptiste Viale
 * 
 */
public class XMLDataLoader implements DataLoader {

	private String folderPath;

	/**
	 * Instantiates a new xML data loader.
	 * 
	 * @param folderPath
	 *            the folder path
	 */
	public XMLDataLoader(String folderPath) {
		this.folderPath = folderPath;
	}

	/**
	 * Load the DataContext using the XML format
	 * 
	 * @param dataContextFileName
	 *            the data context file name
	 * @return the data context loaded
	 */
	@Override
	public DataContext getDataContext(String dataContextFileName) {
		try {
			JAXBContext jc = JAXBContext.newInstance(DataContext.class);
			Unmarshaller unmarshaller = jc.createUnmarshaller();

			DataContext dc = (DataContext) unmarshaller.unmarshal(new File(
					folderPath + "/" + dataContextFileName));
			return dc;
		} catch (JAXBException e) {
			System.err.println("Failed to load data : " + e.getMessage());
			return null;
		}
	}
}
